#pragma once

#include <iostream>
#include <queue>
#include <vector>
#include <pthread.h>
#include <functional>
#include <string>
#include "log.hpp"
#include "Thread.hpp"
#include "LockGoard.hpp"

static const int defaultnum = 5;
class ThreadData
{
public:
    ThreadData(std::string &name) : threadname(name)
    {
    }

    ~ThreadData()
    {
    }
    std::string threadname;
};
template <class T>
class Threadpool
{
private:
    Threadpool(int thread_num = defaultnum) : _thread_num(thread_num)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
        for (int i = 0; i < _thread_num; i++)
        {
            std::string threadname = "Thread-";
            threadname += std::to_string(i + 1);
            ThreadData td(threadname);
            _threads.emplace_back(threadname,
                                 std::bind(&Threadpool::ThreadRun, this, std::placeholders::_1),
                                 td);
            lg.LogMessage(Info, "%s is create...\n", threadname.c_str());
        }
    }
    Threadpool(const Threadpool<T> &tp) = delete;
    const Threadpool<T> &operator=(const Threadpool<T>) = delete;

public:
    // 单例模式
    static Threadpool<T> *GetInstance()
    {
        if (instance == nullptr)
        {
            LockGuard lockguard(&sig_lock);
            if (instance == nullptr)
            {
                lg.LogMessage(Info, "创建单例成功...\n");
                instance = new Threadpool<T>();
            }
        }
        return instance;
    }
    bool Start()
    {
        for (auto &thread : _threads)
        {
            thread.Start();
            lg.LogMessage(Info, "%s is running...\n", thread.ThreadName().c_str());
        }
        return true;
    }
    void ThreadWait(ThreadData &td)
    { // 阻塞
        lg.LogMessage(Debug, "no task,%s is sleeping...\n", td.threadname.c_str());
        pthread_cond_wait(&_cond, &_mutex);
    }
    void ThreadWakeup() // 唤醒
    {
        pthread_cond_signal(&_cond);
    }
    void chenckself()
    {
        // 1. _task_num > _task_num_high_water && _thread_num < _thread_num_high_water
        // 创建更多的线程，并且更新_thread_num

        // 2. _task_num == _task_num_low_water && _thread_num >= _thread_num_high_water
        // 把自己退出了，并且更新_thread_num
    }
    void ThreadRun(ThreadData &td)
    {
        while (true)
        {
            chenckself();
            T t;
            {
                LockGuard lock(&_mutex);
                while (_q.empty())
                {
                    ThreadWait(td);
                    lg.LogMessage(Debug, "thread %s is wakeup...\n", td.threadname.c_str());
                    t = _q.front();
                    _q.pop();
                }
            }
            t();
            lg.LogMessage(Debug, "% s handler task % s done, result is : % s\n ",
                          td.threadname,t.PrintTask().c_str(), t.PrintResult().c_str());
        }
    }
    void Push(T &in)
    {
        lg.LogMessage(Debug, "other thread push a task, task is : %s\n", in.PrintTask().c_str());
        LockGuard lockguard(&_mutex);
        _q.push(in);
        ThreadWakeup();
    }
    ~Threadpool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond);
    }
    void Wait()
    {
        for (auto &thread : _threads)
        {
            thread.Join();
        }
    }

private:
    std::queue<T> _q;
    std::vector<Thread<ThreadData>> _threads;
    int _thread_num;
    pthread_mutex_t _mutex;
    pthread_cond_t _cond;
    // int nuber = 1;

    static Threadpool<T> *instance;
    static pthread_mutex_t sig_lock;
};

template <class T>
Threadpool<T> *Threadpool<T>::instance = nullptr;
template <class T>
pthread_mutex_t Threadpool<T>::sig_lock = PTHREAD_MUTEX_INITIALIZER;